<?php
namespace app\api\controller;

class BillRepayRecord extends Permission
{
    /** 
     * status
     * 0 = 待审核
     * 20 = 已通过
     * 90 = 未通过
     * */
    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $limit = isset($this->input_data['limit']) ? $this->input_data['limit'] : 15;
        $map = [];

        if( isset($this->input_data['status']) && $this->input_data['status'] != 99999999) $map['a.status'] = $this->input_data['status'];
        if( isset($this->input_data['bill_id']) && $this->input_data['bill_id'] > 0) $map['a.bill_id'] = $this->input_data['bill_id'];

        if( isset($this->input_data['type']) && $this->input_data['type'] != '全部') $map['type'] = ['LIKE','%'.$this->input_data['type'].'%'];
        if( isset($this->input_data['cate']) && $this->input_data['cate'] != '全部') $map['cate'] = ['LIKE','%'.$this->input_data['cate'].'%'];

        $total = db('BillRepayRecord')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->field('a.*,b.area,b.company,b.plate_number,b.realname,b.tel')->where($map)->count();
        $rows = db('BillRepayRecord')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->field('a.*,b.area,b.company,b.plate_number,b.realname,b.tel')->order('id', 'DESC')->page($page,$limit)->where($map)->select();

        $list = [];
        foreach ($rows as $v) {
            $v = $this->_getStatus($v);

            $v['amount'] = formatMoneyShow($v['amount']);

            $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
            $v['judge_time'] = date('Y-m-d H:i',$v['judge_time']);
            $v['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
            
            $list[] = $v;
        }
        
        $data = [
            'total' => $total,
            'list' => $list
        ];
        $this->resSuc($data);
    }
  
    private function _getStatus($v)
    {
        $text = [];
        $text[0] = '待审核';
        $text[200] = '已完成';
        $text[30] = '未通过';

        $type = [];
        $type[0] = 'info';
        $type[200] = 'success';
        $type[30] = 'danger';

        $v['statusText'] = $text[$v['status']];
        $v['statusType'] = $type[$v['status']];

        return $v;
    }

}
